package org.chenmin

import javax.sql.DataSource
import java.sql.Connection
import java.sql.Statement

class SqlService {

    DataSource dataSource

    def sqlList(String sql){
        def sList = []
        def sqlN = sql.split("\n")
        String sqlL = ""
        sqlN.each{ l->
            if (!l.startsWith("/*")&&!l.startsWith("#")&&l.indexOf("/*")==-1){
                sqlL = sqlL + l +"\n"
            }
        }
        def sqlS = sqlL.split(";")
        sqlS.each{ l->
            l = l.trim()
            if (l.length()!=0){
                sList << l
                println l
            }
        }
        return  sList
    }

    def executeBatch(sList){
        def sMap = [:]
        Connection conn = dataSource.connection
        Statement stmt = conn.createStatement()
        sList.each{l->
            stmt.addBatch(l)
        }
        int[] c = stmt.executeBatch()
        int cc = 0
        sList.each{l->
            sMap.put(l,c[cc])
            cc++
        }
        stmt.close()
        conn.close()
        return sMap
    }
}
